//
//  ReadingListViewController.m
//  村医
//
//  Created by 融医网 on 2017/6/7.
//  Copyright © 2017年 融医联盟. All rights reserved.
//

#import "YCReadingListViewController.h"
#import "YCReadingListTableViewCell.h"
#import "YCReadingModel.h"
#import "YCOrderDetailViewController.h"
#import "PayOrderViewController.h"


@interface YCReadingListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray<YCReadingModel *> *dataArrayM;
@property (nonatomic, assign)NSInteger page;

@end

@implementation YCReadingListViewController

-(NSMutableArray *)dataArrayM{
    if (!_dataArrayM) {
        _dataArrayM = [NSMutableArray array];
    }
    return _dataArrayM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.page = 1;
        [self.dataArrayM removeAllObjects];
        [self updataDate];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
         self.page ++;
         [self updataDate];
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YCReadingListTableViewCell" bundle:nil] forCellReuseIdentifier:@"YCReadingListTableViewCell"];
        
    self.navigationItem.title = @"阅片列表";
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.page = 1;
    [self.dataArrayM removeAllObjects];
    [self updataDate];
}

-(void)updataDate{
    [self presentLoadingTips:@"正在加载"];
    NSString *url = readingOrderList;
    //[YCUserTool sharedTools].linkMobile
    NSDictionary *params = @{
                             @"page_num":[NSString stringWithFormat:@"%ld",self.page],
                             @"link_mobile":[YCUserTool sharedTools].linkMobile,
                             @"yw_id":yw_id,
                             };
    
    [DownloadData post:url params:params success:^(id json) {
        if (json) {
            
            NSArray *jsonArr = (NSArray *)json;
            if (jsonArr.count == 0 && self.page == 1) {
                [self noDataViewShowWithSuperView:self.view andTitle:@"暂无数据"];
            }
            
            [self.dataArrayM addObjectsFromArray:[YCReadingModel mj_objectArrayWithKeyValuesArray:json]];
            [self.tableView reloadData];
            
            [self dismissTips];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSError *error) {
        
        [self dismissTips];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];

}

#pragma mark - tableViewDelegate,tableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArrayM.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YCReadingListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YCReadingListTableViewCell" forIndexPath:indexPath];
    
    [cell configureWithReadingModel:self.dataArrayM[indexPath.row] delataOrder:^() {
        
        NSLog(@"删除订单 %ld",indexPath.row);
    } pay:^() {
        
        
        PayOrderViewController * payVC = [[PayOrderViewController alloc]init];
        
        payVC.readingModel =  self.dataArrayM[indexPath.row]  ;
        
        [self.navigationController pushViewController:payVC animated:YES];
            
        
    }];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //判断订单的状态，如果不显示按钮，高度返回121；
    CGFloat height = 0;
    if ([self.dataArrayM[indexPath.row].orderStatus isEqualToString:@"待付款"]) {
        height = 205;
    }else{
        height = 161;
    }
    return height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YCReadingModel *model = self.dataArrayM[indexPath.row];
    
    if ([model.orderStatus isEqualToString:@"待付款"]) {
        
        PayOrderViewController * payVC = [[PayOrderViewController alloc]init];
        
        payVC.readingModel =  self.dataArrayM[indexPath.row]  ;
        
        [self.navigationController pushViewController:payVC animated:YES];

    }else{
    
        YCOrderDetailViewController *orderVc = [YCOrderDetailViewController new];
        
        orderVc.model = model;
        
        [self.navigationController pushViewController:orderVc animated:YES];
    }
    

}


@end
